/**
 * @export
 * @class AnimationManager
 * @desc 动画管理器
 * @example
 * let manager = new AnimationManager()
 * manager.regist('update', function() {})
 */
import { requestAnimFrame, cancelAnimFrame } from './util'
export default class AnimationManager {
  constructor() {
    /** @member {Array} - 用于存放所有的动画对象 */
    this.animationHandlers = []
    
    /** @member {Boolean} - 用于配置当前动画管理器中存放的所有动画的执行状态 */
    this.isRunning = true

    /** @member {Number}} - 节流配置 */
    this.throttle = 0

    /** @member {Date} - 上一次执行的时间戳, 用于节流支持 */
    this.lastExcuteTime = Date.now()
    
    /** @member {Function} - 自回调函数 */
    this.raf = null

    // 运行管理器
    this.excute()
  }

  excute() {
    const _this = this
    this.raf = requestAnimFrame(() => {
      if (!_this.isRunning) {
        _this.excute()
        return
      }

      if (_this.throttle > 0) {
        const curTime = Date.now()
        if (curTime - _this.lastExcuteTime < _this.throttle) {
          _this.excute()
          return
        }
        _this.lastExcuteTime = curTime
      }
      for (let i = 0; i < _this.animationHandlers.length; i++) {
        if (_this.animationHandlers[i].executable) {
          _this.animationHandlers[i].func()
        }
      }

      _this.excute()
    })
  }

  // 停止管理器运行
  stop(time) {
    this.isRunning = false

    if (time && Number.isInteger(time)) {
      const _this = this
      setTimeout(() => {
        _this.isRunning = true
      }, time)
    }
  }

  // 开启管理器运行
  run() {
    this.isRunning = true
  }

  // 注册新的动画
  regist(name, func) {
    if (Object.prototype.toString.call(name) !== '[object String]' || Object.prototype.toString.call(func) !== '[object Function]') {
      return false
    }
    for (let i = 0; i < this.animationHandlers.length; i++) {
      if (this.animationHandlers[i].name === name) {
        return false
      }
    }

    const animateHandler = {
      name: name,
      func: func,
      executable: true
    }
    if (arguments.length > 2) {
      // bind options
    }
    this.animationHandlers.push(animateHandler)
    return true
  }

  // 注册新的动画保存到管理器队列最前面(最早执行)
  unshift(name, func) {
    if (Object.prototype.toString.call(name) !== '[object String]' || Object.prototype.toString.call(func) !== '[object Function]') {
      return false
    }

    for (let i = 0; i < this.animationHandlers.length; i++) {
      if (this.animationHandlers[i].name === name) {
        return false
      }
    }

    const animateHandler = {
      name: name,
      func: func,
      executable: true
    }
    if (arguments.length > 2) {
      // bind options
    }
    this.animationHandlers.unshift(animateHandler)
    return true
  }

  // 删除对应名称动画
  unload(name) {
    for (let i = 0; i < this.animationHandlers.length; i++) {
      if (this.animationHandlers[i].name === name) {
        this.animationHandlers.splice(this.animationHandlers.indexOf(this.animationHandlers[i]), 1)
        return true
      }
    }

    return false
  }

  // 暂停动画执行
  pause(name) {
    for (let i = 0; i < this.animationHandlers.length; i++) {
      if (this.animationHandlers[i].name === name) {
        this.animationHandlers[i].executable = false
        return true
      }
    }

    return false
  }

  // 重新开始动画执行
  resume(name) {
    for (let i = 0; i < this.animationHandlers.length; i++) {
      if (this.animationHandlers[i].name === name) {
        this.animationHandlers[i].executable = true
        return true
      }
    }

    return false
  }

  // 设置节流
  setThrottle(value) {
    this.throttle = value
  }

  // 销毁管理器, 销毁动画
  destroy() {
    cancelAnimFrame(this.raf)
  }
}
